unit clsAtrib_Maquina;

interface

uses dm,dbtables,biblio,sysutils;

type
  TAtrib_Maquina=Class(TObject)
    public
      Hist_Codigo: string;
      Descricao  : string;
      Percentual : double;
      Operacao   : string;
      function  Recuperar(Cod_AtribM: string): boolean;
      function  Retorna_Ultimo               : string;
      procedure Gravar(Cod_AtribM: String);
      procedure Excluir(Cod_AtribM: String);
      procedure Consultar(p_Descricao: string; var tq: TQuery);
    end;

implementation

procedure TAtrib_Maquina.Gravar;
var tq: TQuery;
begin
 tq             := Tquery.Create(DmInt);
 tq.databasename:= dmint.Db.DatabaseName;
 tq.active      := False;
 tq.sql.clear;
 tq.params.clear;
 tq.sql.add('Select * from Atrib_Maquina where Cod_AtribM=:a');
 tq.params[0].asstring:= Cod_AtribM;
 tq.open;
 if tq.eof then begin
  tq.close;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Insert into Atrib_Maquina values ('+
             ':k_Cod_AtribM ,');
  if not vazio(Hist_Codigo) then tq.sql.add(':k_Hist_Codigo,') else tq.sql.add('null,');
  tq.sql.add(':k_Descricao  ,'+
             ':k_Percentual ,'+
             ':k_Operacao   )');
 end else begin
  tq.close;
  tq.sql.clear;
  tq.params.clear;
  tq.sql.add('Update Atrib_Maquina set   ');
  if not vazio(Hist_Codigo) then tq.sql.add('Hist_Codigo=:k_Hist_Codigo,') else tq.sql.add('Hist_Codigo=null,');
  tq.sql.add('Descricao  =:k_Descricao  ,'+
             'Percentual =:k_Percentual ,'+
             'Operacao   =:k_Operacao    '+
             'where                      '+
             'Cod_AtribM =:k_Cod_AtribM  ');
 end;
 tq.parambyname('k_Cod_AtribM').asstring := Cod_AtribM;
 if not vazio(Hist_Codigo) then tq.parambyname('k_Hist_Codigo').asstring:= Hist_Codigo;
 tq.parambyname('k_Descricao').asstring  := Descricao;
 tq.parambyname('k_Percentual').asfloat  := Percentual;
 tq.parambyname('k_Operacao').asstring   := Operacao;
 tq.execsql;
 tq.close;
 tq.Free;
end;

procedure TAtrib_Maquina.Excluir;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Delete from Atrib_Maquina where Cod_AtribM=:a');
 tq.params[0].asstring:= Cod_AtribM;
 tq.execsql;
 tq.close;
 tq.Free;
end;

function TAtrib_Maquina.Recuperar;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Select * from Atrib_Maquina where Cod_AtribM=:a');
 tq.params[0].asstring:= Cod_AtribM;
 tq.open;
 if tq.eof then result:= false else result:= true;
 Hist_Codigo:= tq.fieldbyname('Hist_Codigo').asstring;
 Descricao  := tq.fieldbyname('Descricao').asstring;
 Percentual := tq.fieldbyname('Percentual').asfloat;
 Operacao   := tq.fieldbyname('Operacao').asstring;
 tq.close;
 tq.free;
end;

function TAtrib_Maquina.Retorna_Ultimo;
var tq: TQuery;
begin
 tq             := Tquery.Create(dmint);
 tq.databasename:= dmint.db.databasename;
 tq.active      := False;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Select max(Cod_AtribM) ultimo from Atrib_Maquina');
 tq.open;
 if tq.fieldbyname('ultimo').asstring='' then
  result:= '001'
 else
  result:= strzero(tq.fieldbyname('ultimo').asinteger+1,3);
 tq.close;
 tq.Free;
end;

procedure TAtrib_Maquina.Consultar;
begin
 tq.close;
 tq.sql.Clear;
 tq.params.clear;
 tq.sql.add('Select * from Atrib_Maquina where Descricao>=:a order by Descricao');
 if p_Descricao='' then p_Descricao:= ' ';
 tq.params[0].asstring:= p_Descricao;
 tq.open;
end;

end.
